#include <vector>
#include <algorithm>
#include <unordered_map>
using namespace std;
class Solution
{
public:
    int findMaxLength(vector<int> &nums)
    {
        unordered_map<int, int> sumMap;
        int sum = 0;
        int ans = 0;
        sumMap[0] = 0;
        for (int i = 0; i < nums.size(); ++i)
        {
            // cout << sumMap[0] << endl;
            sum += nums[i];
            if (sumMap.count(2 * sum - (i + 1)))
                ans = max(ans, (i + 1) - sumMap[2 * sum - (i + 1)]);
            else
                sumMap[2 * sum - (i + 1)] = i + 1;
        }
        return ans;
    }
};